import Vue from 'vue'
import VueRouter from 'vue-router'
import {isMobile} from "@/utils/utils"
Vue.use(VueRouter)

const computerRouter =  [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/login',  // 重定向到登录页
    children: [
      { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/manager/403') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin') },
      { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
      { path: 'notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice') },
      { path: 'user', name: 'User', meta: { name: '用户信息' }, component: () => import('../views/manager/User') },
      { path: 'category', name: 'Category', meta: { name: '分类信息' }, component: () => import('../views/manager/Category') },
      { path: 'goods', name: 'Goods', meta: { name: '商品信息' }, component: () => import('../views/manager/Goods') },
      { path: 'circles', name: 'Circles', meta: { name: '圈子信息' }, component: () => import('../views/manager/Circles') },
      { path: 'posts', name: 'Posts', meta: { name: '社区帖子' }, component: () => import('../views/manager/Posts') },
      { path: 'help', name: 'Help', meta: { name: '求购信息' }, component: () => import('../views/manager/Help') },
      { path: 'address', name: 'Address', meta: { name: '收货地址' }, component: () => import('../views/manager/Address') },
      { path: 'feedback', name: 'Feedback', meta: { name: '反馈信息' }, component: () => import('../views/manager/Feedback') },
      { path: 'comment', name: 'Comment', meta: { name: '评论信息' }, component: () => import('../views/manager/Comment') },
      { path: 'orders', name: 'Orders', meta: { name: '订单信息' }, component: () => import('../views/manager/Orders') },
    ]
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/computer/Front.vue'),
    children: [
      { path: 'home', component: () => import('../views/front/computer/Home.vue') },
      { path: 'person', component: () => import('../views/front/computer/Person.vue') },
      { path: 'goodsDetail', component: () => import('../views/front/computer/GoodsDetail.vue'),meta: {showtab:false}},
      { path: 'collect', component: () => import('../views/front/computer/Collect.vue') },
      { path: 'notice', component: () => import('../views/front/computer/Notice.vue') },
      { path: 'addGoods', component: () => import('../views/front/computer/AddGoods.vue') },
      { path: 'goods', component: () => import('../views/front/computer/Goods.vue') },
      { path: 'address', component: () => import('../views/front/computer/Address.vue') },
      { path: 'orders', component: () => import('../views/front/computer/Orders.vue') },
      { path: 'posts', component: () => import('../views/front/computer/Posts.vue') },
      { path: 'userPosts', component: () => import('../views/front/computer/UserPosts.vue') },
      { path: 'postsDetail', component: () => import('../views/front/computer/PostsDetail.vue') },
      { path: 'help', component: () => import('../views/front/computer/Help.vue') },
      { path: 'userHelp', component: () => import('../views/front/computer/UserHelp.vue') },
      { path: 'feedback', component: () => import('../views/front/computer/Feedback.vue') },
      { path: 'userFeedback', component: () => import('../views/front/computer/UserFeedback.vue') },
      { path: 'chat', component: () => import('../views/front/computer/Chat.vue') },
      { path: 'search', component: () => import('../views/front/computer/Search.vue') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
  { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

const mobileRouter =  [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/login',  // 重定向到登录页
    children: [
      { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/manager/403') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin') },
      { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
      { path: 'notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice') },
      { path: 'user', name: 'User', meta: { name: '用户信息' }, component: () => import('../views/manager/User') },
      { path: 'category', name: 'Category', meta: { name: '分类信息' }, component: () => import('../views/manager/Category') },
      { path: 'goods', name: 'Goods', meta: { name: '商品信息' }, component: () => import('../views/manager/Goods') },
      { path: 'circles', name: 'Circles', meta: { name: '圈子信息' }, component: () => import('../views/manager/Circles') },
      { path: 'posts', name: 'Posts', meta: { name: '社区帖子' }, component: () => import('../views/manager/Posts') },
      { path: 'help', name: 'Help', meta: { name: '求购信息' }, component: () => import('../views/manager/Help') },
      { path: 'address', name: 'Address', meta: { name: '收货地址' }, component: () => import('../views/manager/Address') },
      { path: 'feedback', name: 'Feedback', meta: { name: '反馈信息' }, component: () => import('../views/manager/Feedback') },
      { path: 'comment', name: 'Comment', meta: { name: '评论信息' }, component: () => import('../views/manager/Comment') },
      { path: 'orders', name: 'Orders', meta: { name: '订单信息' }, component: () => import('../views/manager/Orders') },
    ]
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/mobile/Front.vue'),
    children: [
      { path: 'home', component: () => import('../views/front/mobile/Home.vue') },
      { path: 'person', component: () => import('../views/front/mobile/Person.vue') },
      { path: 'goodsDetail', component: () => import('../views/front/mobile/GoodsDetail.vue'),meta: {showTab:false} },
      { path: 'collect', component: () => import('../views/front/mobile/Collect.vue') },
      { path: 'notice', component: () => import('../views/front/mobile/Notice.vue') },
      { path: 'addGoods', component: () => import('../views/front/mobile/AddGoods.vue') },
      { path: 'goods', component: () => import('../views/front/mobile/Goods.vue') },
      { path: 'address', component: () => import('../views/front/mobile/Address.vue') },
      { path: 'orders', component: () => import('../views/front/mobile/Orders.vue') },
      { path: 'posts', component: () => import('../views/front/mobile/Posts.vue') },
      { path: 'userPosts', component: () => import('../views/front/mobile/UserPosts.vue') },
      { path: 'postsDetail', component: () => import('../views/front/mobile/PostsDetail.vue') },
      { path: 'help', component: () => import('../views/front/mobile/Help.vue') },
      { path: 'userHelp', component: () => import('../views/front/mobile/UserHelp.vue') },
      { path: 'feedback', component: () => import('../views/front/mobile/Feedback.vue') },
      { path: 'userFeedback', component: () => import('../views/front/mobile/UserFeedback.vue') },
      { path: 'chat', component: () => import('../views/front/mobile/Chat.vue') },
      { path: 'search', component: () => import('../views/front/mobile/Search.vue') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
  { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]


// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

let routes;
// const routes = [
//   {
//     path: '/',
//     name: 'Manager',
//     component: () => import('../views/Manager.vue'),
//     redirect: '/login',  // 重定向到登录页
//     children: [
//       { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/manager/403') },
//       { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
//       { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin') },
//       { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
//       { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
//       { path: 'notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice') },
//       { path: 'user', name: 'User', meta: { name: '用户信息' }, component: () => import('../views/manager/User') },
//       { path: 'category', name: 'Category', meta: { name: '分类信息' }, component: () => import('../views/manager/Category') },
//       { path: 'goods', name: 'Goods', meta: { name: '商品信息' }, component: () => import('../views/manager/Goods') },
//       { path: 'circles', name: 'Circles', meta: { name: '圈子信息' }, component: () => import('../views/manager/Circles') },
//       { path: 'posts', name: 'Posts', meta: { name: '社区帖子' }, component: () => import('../views/manager/Posts') },
//       { path: 'help', name: 'Help', meta: { name: '求购信息' }, component: () => import('../views/manager/Help') },
//       { path: 'address', name: 'Address', meta: { name: '收货地址' }, component: () => import('../views/manager/Address') },
//       { path: 'feedback', name: 'Feedback', meta: { name: '反馈信息' }, component: () => import('../views/manager/Feedback') },
//       { path: 'comment', name: 'Comment', meta: { name: '评论信息' }, component: () => import('../views/manager/Comment') },
//       { path: 'orders', name: 'Orders', meta: { name: '订单信息' }, component: () => import('../views/manager/Orders') },
//     ]
//   },
//   {
//     path: '/front',
//     name: 'Front',
//     component: () => import('../views/Front.vue'),
//     children: [
//       { path: 'home', component: () => import('../views/front/computer/Home.vue') },
//       { path: 'person', component: () => import('../views/front/computer/Person.vue') },
//       { path: 'goodsDetail', component: () => import('../views/front/computer/GoodsDetail.vue') },
//       { path: 'collect', component: () => import('../views/front/computer/Collect.vue') },
//       { path: 'notice', component: () => import('../views/front/computer/Notice.vue') },
//       { path: 'addGoods', component: () => import('../views/front/computer/AddGoods.vue') },
//       { path: 'goods', component: () => import('../views/front/computer/Goods.vue') },
//       { path: 'address', component: () => import('../views/front/computer/Address.vue') },
//       { path: 'orders', component: () => import('../views/front/computer/Orders.vue') },
//       { path: 'posts', component: () => import('../views/front/computer/Posts.vue') },
//       { path: 'userPosts', component: () => import('../views/front/computer/UserPosts.vue') },
//       { path: 'postsDetail', component: () => import('../views/front/computer/PostsDetail.vue') },
//       { path: 'help', component: () => import('../views/front/computer/Help.vue') },
//       { path: 'userHelp', component: () => import('../views/front/computer/UserHelp.vue') },
//       { path: 'feedback', component: () => import('../views/front/computer/Feedback.vue') },
//       { path: 'userFeedback', component: () => import('../views/front/computer/UserFeedback.vue') },
//       { path: 'chat', component: () => import('../views/front/computer/Chat.vue') },
//       { path: 'search', component: () => import('../views/front/computer/Search.vue') },
//     ]
//   },
//   { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
//   { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
//   { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
// ]

if (isMobile()) {
  routes = mobileRouter
} else {
  routes = computerRouter
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 注：不需要前台的项目，可以注释掉该路由守卫
// 路由守卫
// router.beforeEach((to ,from, next) => {
//   let user = JSON.parse(localStorage.getItem("xm-user") || '{}');
//   if (to.path === '/') {
//     if (user.role) {
//       if (user.role === 'USER') {
//         next('/front/home')
//       } else {
//         next('/home')
//       }
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
